home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / ada / adaed-1.11 / adaed-1 / Adaed-1.11.0a / action.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-07  |  3.1 KB  |  102 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /* action.c - temporarily pulled out from adaprs.c */
  10.  
  11. #include "ada.h"
  12. #include "miscprots.h"
  13. #include "actionprots.h"
  14.  
  15.  
  16. /* Action: Return an action corresponding to a given state and input 
  17.    symbol. Given a state and input symbol, we get a unique value uniquevalue,
  18.    which we use to get a hash value hashvalue. We use hashvalue as an index
  19.    into act_tab1. If the entry is 0, we return the default action for the
  20.    state. If the table entry is < NUM_ACTIONS, then we return either
  21.    the table entry if act_tab2 gives uniquenum at location hashvalue, or
  22.    the default reduction if not. Otherwise we go through act_tab2 starting
  23.    at index uniquevalue until we find uniquevalue or 0, and return what
  24.    is found in act_tab1 at this location or the default reduction,
  25.    respectively. A shift is indicated by the new state to go to after the
  26.    shift, and a reduction is indicated by the rule number + NUM_STATES. */
  27.  
  28.  
  29. int action(int state, int sym)                                    /*;action*/
  30. {
  31.     long uniquenum;
  32.     int hashvalue;
  33.     int i;
  34.     int aval;
  35. #ifdef DEBUGACT
  36.     printf("action state %d sym %d\n", state, sym); /*DEBUG*/
  37. #endif
  38.     if (state < 0) {
  39. #ifdef DEBUG
  40.         printf("action: fatal error state negative %d\n", state);
  41. #endif
  42.         exitp(RC_INTERNAL_ERROR);
  43.     }
  44.     if (sym < 0) {
  45. #ifdef DEBUG
  46.         printf("action: fatal error sym negative %d\n", sym);
  47. #endif
  48.         exitp(RC_INTERNAL_ERROR);
  49.     }
  50. #ifdef DEBUGACT
  51.     uniquenum = state;
  52.     printf("uniq = state %ld\n", uniquenum);
  53.     uniquenum *= NUM_INPUTS;
  54.     printf("uniq = NUM_INT %ld\n", uniquenum);
  55.     uniquenum += sym;
  56.     printf("uniq += sym %ld\n", uniquenum);
  57.     hashvalue = uniquenum % TABLE_SIZE;
  58.     printf("action hash long %ld becomes %d\n", uniquenum, hashvalue);
  59.     aval = act_tab1[hashvalue];
  60.     printf("initial act_tab1 value for %d is %d\n", hashvalue, aval);
  61.     if (act_tab1[hashvalue] == 0) {
  62.         aval = def_action[state - 1];
  63.         printf("action 1st case %d\n", aval);
  64.         return(aval);
  65.     }
  66.     if (act_tab1[hashvalue] < NUM_ACTIONS) {
  67.         if (act_tab2[hashvalue]== uniquenum) {
  68.             aval = act_tab1[hashvalue];
  69.             printf("action 2nd case %d\n", aval);
  70.             return aval;
  71.         }
  72.         else {
  73.             aval = def_action[state-1];
  74.             printf("acton 3rd case %d\n", aval);
  75.             return aval;
  76.         }
  77.     }
  78.     for (i = act_tab1[hashvalue] - NUM_ACTIONS; act_tab2[i] != 0; i++) {
  79.         if (act_tab2[i] == uniquenum) {
  80.             aval = act_tab1[i];
  81.             printf("action 4th case %d %d\n", i, aval);
  82.             return aval;
  83.         }
  84.     }
  85.     aval = def_action[state-1];
  86.     printf("action 5th case %d\n", aval);
  87.     return(aval);
  88. #else
  89.     uniquenum = (long) state * NUM_INPUTS + sym;
  90.     hashvalue = (int)uniquenum % TABLE_SIZE;
  91.     aval = act_tab1[hashvalue];
  92.     if (aval == 0)
  93.         return(def_action[state - 1]);
  94.     if (aval < NUM_ACTIONS)
  95.         return((act_tab2[hashvalue] == uniquenum) ? aval : def_action[state-1]);
  96.     for (i = act_tab1[hashvalue] - NUM_ACTIONS; act_tab2[i] != 0; i++)
  97.         if (act_tab2[i] == uniquenum)
  98.             return(act_tab1[i]);
  99.     return(def_action[state - 1]);
  100. #endif
  101. }
  102.